Rainier.UI.V2.TextDiamondFigure = draw2d.shape.basic.Diamond.extend({
  NAME: "Rainier.UI.V2.TextDiamondFigure",

  /**
   * @名称
   * @功能   初始化器件的参数值
   * @author 李姣
   * @data   2019-05-23
   * @param  {[type]}   modelProperties [description]
   * @return {[type]}                   [description]
   */
  init: function (modelProperties, setter, getter) {
    var _this = this;
    this.properties = modelProperties;
    this.uiProperty = modelProperties.uiProperty;
    var attrs = {
      stroke: modelProperties.uiProperty.stroke,
      color: modelProperties.uiProperty.color,
      width: modelProperties.uiProperty.width,
      height: modelProperties.uiProperty.height,
      // "svgString" : modelProperties.uiProperty.imageString,
      bgColor: modelProperties.uiProperty.bgColor,
      radius: modelProperties.uiProperty.radius,
    };
    this.symbolLabelFigure = null; //符号图元

    this._super(attrs);
    var text = modelProperties.modelProperty.text;
    // var dotNum = this.getDotNum(text);
    // if(dotNum <= 8){
    this.label = new draw2d.shape.basic.Text({
      text: text,
      bold: this.uiProperty.bold,
      fontColor: this.uiProperty.color,
      fontSize: this.uiProperty.fontSize,
      fontFamily: this.uiProperty.fontFamily,
      stroke: 0,
      width: this.uiProperty.width - 9,
      height: this.uiProperty.height / 2,
    });
    // }else{
    // this.label = new draw2d.shape.basic.Text({text:text, bold:this.uiProperty.bold, fontColor:this.uiProperty.color,fontSize:this.uiProperty.fontSize,fontFamily:this.uiProperty.fontFamily, stroke:0,width:this.uiProperty.width-9,height:this.uiProperty.height/2});
    // }

    this.label.on("change:width", $.proxy(this.widthChanged, this));

    this.label.setWidth(this.uiProperty.width - 54);

    // add the new decoration to the connection with a position locator.
    //
    this.add(this.label, new draw2d.layout.locator.CenterLocator(this));

    this.label.on("change:text", $.proxy(this.valueChanged, this));
    // this.label.installEditor(new draw2d.ui.LabelInplaceEditor());

    var policyAttrs = { recBgColor: "#0BF60E", lineColor: "#2C70FF" };
    this.installEditPolicy(new Rainier.UI.V2.RectangleSelectionFeedbackPolicy(policyAttrs));
    this.on("resize", $.proxy(this.textFigureResize, this));
  },

  /**
   * @名称   setSvgString
   * @功能   设置模型的 svg 图片
   * @author 杨顺强
   * @data   2017-09-21
   * @param  {[type]}   svgString [description]
   */
  setSvgString: function (svgString) {
    this.svgString = svgString;
    var attrs = {
      src: svgString,
    };
    // this.repaint(attrs);
    var canvas = this.canvas;

    //此处重在重新计算 图元需要显示的图形
    this.setCanvas(null);
    this.setCanvas(canvas);
    this.setPosition(this.x, this.y);
  },

  /**
   * @method
   * Set the position of the object.
   *
   *      // Alternatively you can use the attr method:
   *      figure.attr({
   *        x: x,
   *        y: y
   *      });
   *
   * @param {Number|draw2d.geo.Point} x The new x coordinate of the figure or the x/y coordinate if it is an draw2d.geo.Point
   * @param {Number} [y] The new y coordinate of the figure
   **/
  setPosition: function (x, y) {
    // this._super(x,y);
    if (typeof x === "undefined") {
      debugger;
    }

    var oldPos = { x: this.x, y: this.y };

    if (x instanceof draw2d.geo.Point) {
      this.x = x.x;
      this.y = x.y;
    } else {
      this.x = x;
      this.y = y;
    }

    var _this = this;

    this.editPolicy.each(function (i, e) {
      if (e instanceof draw2d.policy.figure.DragDropEditPolicy) {
        var newPos = e.adjustPosition(_this, _this.x, _this.y);
        _this.x = newPos.x;
        _this.y = newPos.y;
      }
    });

    this.repaint();

    // don't fire any events or update the ResizeHandles if nothing has happen
    //
    //  if(oldPos.x!=this.x || oldPos.y!=this.y)
    {
      // Update the resize handles if the user change the position of the
      // element via an API call.
      //
      this.editPolicy.each(function (i, e) {
        if (e instanceof draw2d.policy.figure.DragDropEditPolicy) {
          e.moved(_this.canvas, _this);
        }
      });

      var dx = this.x - oldPos.x;
      var dy = this.y - oldPos.y;
      this.translate(dx, dy);
      this.fireEvent("move", { figure: this, dx: dx, dy: dy });
      this.fireEvent("change:x", { figure: this, dx: dx });
      this.fireEvent("change:y", { figure: this, dy: dy });
    }
    this.repaint();
    return this;
  },

  /**
   * @名称   repaint
   * @功能   绘制图元
   * @author 杨顺强
   * @data   2017-09-21
   * @param  {[type]}   attributes [description]
   * @return {[type]}              [description]
   */
  repaint: function (attributes) {
    this._super(attributes);
  },

  /**
   * @名称   createSet
   * @功能   创建需要绘制的图形集合
   * @author 杨顺强
   * @data   2017-09-21
   * @return {[type]}   [description]
   */
  createSet: function () {
    var bgImage = this._super();
    var set = this.canvas.paper.set();
    if (bgImage) {
      set.push(bgImage);
    }
    set.attr({ stroke: 2 });

    return set;
  },

  /**
   * @名称   valueChanged
   * @功能   label值发生变化时，向外发出事件
   * @author 杨顺强
   * @data   2018-01-31
   * @param  {[type]}   changedObj [description]
   * @return {[type]}              [description]
   */
  valueChanged: function (eventSource, paramObj) {
    console.log(paramObj);
    this.fireEvent("change:text", paramObj);
  },

  /**
   * @名称   fontSizeChanged
   * @功能   label字号发生变化时，向外发出事件
   * @author 李姣
   * @data   2019-05-09
   * @param  {[type]}   changedObj [description]
   * @return {[type]}              [description]
   */
  fontSizeChanged: function (eventSource, paramObj) {
    this.fireEvent("change:fontSize", paramObj);
  },

  /**
   * @名称   dimensionChanged
   * @功能   label宽度高度发生变化时，向外发出事件
   * @author 李姣
   * @data   2019-05-14
   * @param  {[type]}   changedObj [description]
   * @return {[type]}              [description]
   */
  dimensionChanged: function (eventSource, paramObj) {
    // console.log(paramObj);
    this.fireEvent("change:dimension", paramObj);
  },

  /**
   * @名称   widthChanged
   * @功能   label宽度发生变化时，向外发出事件
   * @author 李姣
   * @data   2019-05-13
   * @param  {[type]}   changedObj [description]
   * @return {[type]}              [description]
   */
  widthChanged: function (eventSource, paramObj) {
    // console.log(paramObj);
    this.fireEvent("change:width", paramObj);
  },

  /**
   * @名称   heightChanged
   * @功能   label高度发生变化时，向外发出事件
   * @author 李姣
   * @data   2019-05-13
   * @param  {[type]}   changedObj [description]
   * @return {[type]}              [description]
   */
  heightChanged: function (eventSource, paramObj) {
    this.fireEvent("change:height", paramObj);
  },

  /**
   * @名称   textFigureResize
   * @功能   text label值发生变化时，向外发出事件
   * @author 李姣
   * @data   2018-08-31
   * @param  {[type]}   changedObj [description]
   * @return {[type]}              [description]
   */
  textFigureResize: function (eventSource, paramObj) {
    this.fireEvent("resize");
    // this.fireEvent("change:dimension",{value:{width:this.width, height:this.height}});
  },

  /**
   * 函数名称：Rainier.UI.V2.TextFigure.setSymbolLabelFigure<br>
   * 函数功能：设置Label<br>
   * 参数输入：<br>
   * property:属性对象<br>
   * sLocator:定位器构造函数<br>
   * x:Number<br>
   * y:Number<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  setSymbolLabelFigure: function (property, sLocator, x, y) {
    var locator = null;
    if (!sLocator || sLocator === "default") {
      locator = new Rainier.UI.V2.XYAbsFigureLocator(x, y);
    } else {
      locator = eval("new " + sLocator + "();");
      locator.offsetX = x;
      locator.offsetY = y;
    }
    property.stroke = 0;
    this.symbolLabelFigure = new Rainier.UI.V2.DeviceSymbolLabelFigure(property, locator);
    this.add(this.symbolLabelFigure, locator);
  },

  /**
   * 函数名称：Rainier.UI.V2.TextFigure.showLabel<br>
   * 函数功能：显示标签<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  showLabel: function () {
    this.labelVisible = false;
    if (this.symbolLabelFigure) {
      this.symbolLabelFigure.setVisible(true);
    }
  },

  /**
   * 函数名称：Rainier.UI.V2.DeviceBoxFigure.unshowLabel<br>
   * 函数功能：隐藏标签<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  unshowLabel: function () {
    this.labelVisible = false;
    if (this.symbolLabelFigure) {
      this.symbolLabelFigure.setVisible(false);
    }
  },

  /**
   * 函数名称：Rainier.UI.V2.TextFigure.getLabel<br>
   * 函数功能：获取label标签<br>
   * 函数返回：label标签属性<br>
   * 异常信息：无<br>
   */
  getLabel: function () {
    return this.label;
  },

  /**
   * 函数名称：Rainier.UI.V2.TextFigure.getLabel<br>
   * 函数功能：获取label标签中.出现的次数<br>
   * 函数返回：.出现的次数<br>
   * 异常信息：无<br>
   */
  getDotNum: function (text) {
    var count = 0;
    var pos = text.indexOf(".");
    while (pos !== -1) {
      count++;
      pos = text.indexOf(".", pos + 1);
    }
    return count;
  },
});
